using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using System.Runtime.InteropServices;
using IDAL;
using DAL;

namespace ITAM
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();
            string connString = "";
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                connString = Configuration.GetConnectionString("deepin");
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                connString = Configuration.GetConnectionString("win7");
            }
            services.AddDbContext<ITAMContext>(options => options.UseMySql(connString, Microsoft.EntityFrameworkCore.ServerVersion.Parse("10.4.20-mariadb")));

            services.AddTransient<IDeviceRecordService, DeviceRecordService>();
            services.AddTransient<IDeviceCategoryService, DeviceCategoryService>();
            services.AddTransient<IVendorService, VendorService>();
            services.AddTransient<IDepreciationRuleService, DepreciationRuleService>();
            services.AddTransient<IPurchasedChannelService, PurchasedChannelService>();
            services.AddTransient<IExcelService, ExcelService>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
            }

            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
            });
        }
    }
}
